package com.kitty.game.onlinemall.service;

import com.alibaba.fastjson.JSONObject;
import com.kitty.game.activity.service.time.XingGuanHandler;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.equip.model.RoleEquipField;
import com.kitty.game.config.CustomFasion;
import com.kitty.game.config.Medicine;
import com.kitty.game.config.OnlineMall;
import com.kitty.game.config.SpecialItem;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.identity.IdentityType;
import com.kitty.game.onlinemall.MallDataPool;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.shop.ShopDataPool;
import com.kitty.game.utils.Const;
import com.kitty.mina.cache.DataCache;
import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

@Service
@Slf4j
public class MallService {


    @Autowired
    EquipService equipService;

    /**
     * 初始化物品数据
     */
    public void initSpecialItem(List<SpecialItem> list) {
        Map<String, SpecialItem> name2SpecialItem = new HashMap<>(list.size());
        for (SpecialItem specialItem : list) {
            name2SpecialItem.put(specialItem.getKey_name(), specialItem);
        }
        MallDataPool.name2SpecialItem = name2SpecialItem;
    }

    /**
     * 取商城物品
     */
    public OnlineMall getOnlineMallByName(String name) {
        for (Map.Entry<String, OnlineMall> entry : DataCache.NAME_MALL.entrySet()) {
            if (entry.getKey().startsWith(name)) {
                return entry.getValue();
            }
        }
        return null;
    }

    /**
     * 给
     */
    public int getRoleEquipId() {
        return SpringUtils.getIdentityService().getNextId(IdentityType.ROLE_EQUIP);
    }

    /**
     * 物品名称取物品信息
     */
    public SpecialItem getSpecialItem(String name) {
        log.info("name={}", JSONObject.toJSONString(name));
        return MallDataPool.name2SpecialItem.get(name);

    }

    /**
     * 取自定义时装
     */
    public CustomFasion getCustomFashion(String name) {
        return DataCache.NAME_CUSTOMFASION.get(name);
    }


    /**
     * 获取消耗道具
     */
    private void getConsumeItem(RoleEquip roleEquip, boolean isGold, OnlineMall onlineMall, String str, int value) {
        SpecialItem specialItem = getSpecialItem(onlineMall.getName());
        {
            RoleEquipField roleEquipField = new RoleEquipField();
            roleEquipField.setType((short) 1);
            LinkedHashMap<Short, FiedValue> basicAttribute = new LinkedHashMap<>();
            roleEquipField.setField(basicAttribute);
            roleEquip.getFields().put(roleEquipField.getType(), roleEquipField);
            DecimalFormat decimalFormat = new DecimalFormat("#,###");
            basicAttribute.put((short) 264, new FiedValue(264, str + decimalFormat.format(value)));
            basicAttribute.put((short) 306, new FiedValue(306, String.valueOf(roleEquip.getId())));
            basicAttribute.put((short) 1, new FiedValue(1, roleEquip.getName()));
            basicAttribute.put((short) 206, new FiedValue(206, onlineMall.getType()));
            basicAttribute.put((short) 74, new FiedValue(74, onlineMall.getItemtype()));
            basicAttribute.put((short) 84, new FiedValue(84, roleEquip.getId()));
            basicAttribute.put((short) 209, new FiedValue(209, "金色"));
            basicAttribute.put((short) 270, new FiedValue(270, isGold ? 0 : 2));
            basicAttribute.put((short) 203, new FiedValue(203, 1));
            basicAttribute.put((short) 40, new FiedValue(40, specialItem.getIcon()));
            basicAttribute.put((short) 207, new FiedValue(207, 5));
        }
        {
            short type = 0;
            if (onlineMall.getName().contains("血玲珑")) {
                type = 6;
            } else if (onlineMall.getName().contains("法玲珑")) {
                type = 11;
            }
            RoleEquipField roleEquipField = new RoleEquipField();
            roleEquipField.setType((short) 258);
            LinkedHashMap<Short, FiedValue> linkedHashMap = new LinkedHashMap<>();
            roleEquipField.setField(linkedHashMap);
            roleEquip.getFields().put(roleEquipField.getType(), roleEquipField);
            linkedHashMap.put(type, new FiedValue(type, value));
        }
    }

    /**
     * 购买消耗型道具
     */
    public void buyConsumeItem(Role role, OnlineMall onlineMall, boolean isGold, short pos) {
        int value = 0;
        String str = null;
        switch (onlineMall.getName()) {
            case "中级血玲珑·融合":
                str = "剩余血量：";
                value = 100000000;
                break;
            case "中级法玲珑·融合":
                str = "剩余法力：";
                value = 100000000;
                break;
            case "中级血玲珑":
                str = "剩余血量：";
                value = 10000000;
                break;
            case "中级法玲珑":
                str = "剩余法力：";
                value = 10000000;
                break;
            case "袖珍血玲珑":
                str = "剩余血量：";
                value = 1000000;
                break;
            case "袖珍法玲珑":
                str = "剩余法力：";
                value = 1000000;
                break;
            case "血玲珑·融合":
                str = "剩余血量：";
                value = 40000000;
                break;
            case "法玲珑·融合":
                str = "剩余法力：";
                value = 40000000;
                break;
            case "血玲珑":
                str = "剩余血量：";
                value = 4000000;
                break;
            case "法玲珑":
                str = "剩余法力：";
                value = 4000000;
                break;
        }
        RoleEquip roleEquip = new RoleEquip();
        roleEquip.setId(getRoleEquipId());
        roleEquip.setRoleId(role.getRoleId());
        roleEquip.setPosition(pos);
        roleEquip.setName(onlineMall.getName());
        LinkedHashMap<Short, RoleEquipField> linkedHashMap = new LinkedHashMap<>();
        roleEquip.setFields(linkedHashMap);
        getConsumeItem(roleEquip, isGold, onlineMall, str, value);
        if (onlineMall.getItemtype() == 6) {
            roleEquip.setType("装备道具");
        } else if (onlineMall.getItemtype() == 10) {
            roleEquip.setType("宠物道具");
        } else {
            roleEquip.setType("其他道具");
        }
        equipService.add(role, roleEquip);
        equipService.refreshRoleEquip(role, roleEquip);
    }

    /**获取道具*/
    private void getItem(RoleEquip roleEquip, OnlineMall onlineMall, int num, boolean isGold) {
        SpecialItem specialItem = getSpecialItem(onlineMall.getName());
        if (specialItem == null) {
            specialItem = new SpecialItem();
            Medicine medicine = ShopDataPool.getMedicineBy(onlineMall.getName());
            if (medicine != null) {
                specialItem.setIcon(medicine.getIcon());
            } else {
                specialItem.setIcon(9500);
            }
        }
        RoleEquipField roleEquipField = new RoleEquipField();
        roleEquipField.setType((short) 1);
        roleEquipField.setField(new LinkedHashMap<>());
        roleEquip.getFields().put(roleEquipField.getType(), roleEquipField);
        if (onlineMall.getName().equals("羽化丹") || onlineMall.getName().equals("点化丹")) {
            roleEquipField.getField().put((short) 206, new FiedValue(206, onlineMall.getType()));
            roleEquipField.getField().put((short) 74, new FiedValue(74, onlineMall.getItemtype()));
            roleEquipField.getField().put((short) 40, new FiedValue(40, specialItem.getIcon()));
            roleEquipField.getField().put((short) 207, new FiedValue(207, 5));
        } else {
            roleEquipField.getField().put((short) 89, new FiedValue(89, ""));
            roleEquipField.getField().put((short) 205, new FiedValue(205, 50));//需求等级
            roleEquipField.getField().put((short) 261, new FiedValue(261, 0));//locked 不可交易?
            roleEquipField.getField().put((short) 206, new FiedValue(206, onlineMall.getType()));
            roleEquipField.getField().put((short) 74, new FiedValue(74, onlineMall.getItemtype()));
            roleEquipField.getField().put((short) 34, new FiedValue(34,0));
            roleEquipField.getField().put((short) 31, new FiedValue(31,0));
            roleEquipField.getField().put((short) 40, new FiedValue(40, specialItem.getIcon()));
            roleEquipField.getField().put((short) 215, new FiedValue(215, 50));//最大需求等级
            roleEquipField.getField().put((short) 348, new FiedValue(348, 0));//限时道具? limit_use_time
            roleEquipField.getField().put((short) 207, new FiedValue(207, 5));
            roleEquipField.getField().put((short) 264, new FiedValue(264, ""));
            roleEquipField.getField().put((short) 248, new FiedValue(248, 0));// 黑水晶没属性是0,有属性是1
            roleEquipField.getField().put((short) 41, new FiedValue(41, 8));// 黑水晶没属性是0,有属性是1
            roleEquipField.getField().put((short) 3001, new FiedValue(3001, ""));//别名
            roleEquipField.getField().put((short) 887, new FiedValue(887, 0));//到期时间
        }

        if (roleEquip.getName().contains(Const.MIJI)) {
            /**秘笈，修改物品属性*/
            roleEquipField.getField().put((short) 206, new FiedValue(206, 33554564));
        }
        roleEquipField.getField().put((short) 282, new FiedValue(282, Const.ITEM_COMBINED_EX));//叠加类型
        roleEquipField.getField().put((short) 1, new FiedValue(1, onlineMall.getName()));
        roleEquipField.getField().put((short) 209, new FiedValue(209, specialItem.getColor()));
        roleEquipField.getField().put((short) 84, new FiedValue(84, roleEquip.getId()));
        roleEquipField.getField().put((short) 306, new FiedValue(306, String.valueOf(roleEquip.getId())));
        roleEquipField.getField().put((short) 270, new FiedValue(270, isGold ? 0 : 2));
        roleEquipField.getField().put((short) 203, new FiedValue(203, num));

    }


    /**
     * 获得黑水晶
     *
     * @param role
     * @param onlineMall
     * @param num
     * @param isGold
     * @return
     */
    public void buyItem(Role role, OnlineMall onlineMall, int num, boolean isGold, short pos) {
        RoleEquip roleEquip = new RoleEquip();
        roleEquip.setId(getRoleEquipId());
        roleEquip.setRoleId(role.getRoleId());
        roleEquip.setPosition(pos);
        roleEquip.setName(onlineMall.getName());
        roleEquip.setFields(new LinkedHashMap<>());
        getItem(roleEquip, onlineMall, num, isGold);
        if (onlineMall.getItemtype() == 6) {
            roleEquip.setType("装备道具");
        } else if (onlineMall.getItemtype() == 10 || onlineMall.getName().contains("召唤令")){
            roleEquip.setType("宠物道具");
        } else if (onlineMall.getItemtype() == 13) {
            roleEquip.setType("天书");
        } else {
            roleEquip.setType("其他道具");
        }
        equipService.add(role, roleEquip);
        equipService.refreshRoleEquip(role, roleEquip);
    }

    /**获得火眼金睛*/
    private void getPiercingEye(RoleEquip roleEquip, OnlineMall onlineMall, boolean isGold) {
        SpecialItem specialItem = getSpecialItem(onlineMall.getName());
        int value =10;
        if(specialItem.getKey_name().contains("融合")){
            value=100;
        }
        RoleEquipField roleEquipField = new RoleEquipField();
        roleEquipField.setType((short) 1);
        roleEquipField.setField(new LinkedHashMap<>());
        roleEquip.getFields().put(roleEquipField.getType(), roleEquipField);
        roleEquipField.getField().put((short) 282, new FiedValue(282, 0));//叠加类型
        roleEquipField.getField().put((short) 1, new FiedValue(1, onlineMall.getName()));
        roleEquipField.getField().put((short) 209, new FiedValue(209, specialItem.getColor()));
        roleEquipField.getField().put((short) 84, new FiedValue(84, roleEquip.getId()));
        roleEquipField.getField().put((short) 43, new FiedValue(43, value));//最大次数
        roleEquipField.getField().put((short) 42, new FiedValue(42, value));//剩余次数
        roleEquipField.getField().put((short) 270, new FiedValue(270, isGold ? 0 : 2));
        roleEquipField.getField().put((short) 203, new FiedValue(203, 1));
        roleEquipField.getField().put((short) 206, new FiedValue(206, onlineMall.getType()));
        roleEquipField.getField().put((short) 306, new FiedValue(306, String.valueOf(roleEquip.getId())));
        roleEquipField.getField().put((short) 74, new FiedValue(74, onlineMall.getItemtype()));
        roleEquipField.getField().put((short) 40, new FiedValue(40, specialItem.getIcon()));
        roleEquipField.getField().put((short) 207, new FiedValue(207, 5));
    }

    /**
     * 购买火眼
     *
     * @param role
     * @param onlineMall
     * @param isGold
     * @return
     */
    public void buyPiercingEye(Role role, OnlineMall onlineMall, boolean isGold, short pos) {
        SpecialItem specialItem = getSpecialItem(onlineMall.getName());
        if (specialItem == null) {
            specialItem = new SpecialItem();
            Medicine medicine = ShopDataPool.getMedicineBy(onlineMall.getName());
            if (medicine != null) {
                specialItem.setIcon(medicine.getIcon());
            } else {
                specialItem.setIcon(1001);
            }
        }
        RoleEquip roleEquip = new RoleEquip();
        roleEquip.setId(getRoleEquipId());
        roleEquip.setRoleId(role.getRoleId());
        roleEquip.setPosition(pos);
        roleEquip.setName(onlineMall.getName());
        roleEquip.setFields(new LinkedHashMap<>());
        getPiercingEye(roleEquip, onlineMall, isGold);
        roleEquip.setType("其他道具");

        equipService.add(role, roleEquip);
        equipService.refreshRoleEquip(role, roleEquip);
    }

    /**获得天书*/
    private void getBook(RoleEquip roleEquip, OnlineMall onlineMall, int num, boolean isGold,boolean isMan) {
        StringBuffer desc = new StringBuffer();
        SpecialItem specialItem = getSpecialItem(onlineMall.getName());
        RoleEquipField roleEquipField = new RoleEquipField();
        roleEquipField.setType((short) 1);
        roleEquipField.setField(new LinkedHashMap<>());
        roleEquip.getFields().put(roleEquipField.getType(), roleEquipField);

        if (!onlineMall.getName().equals("天书")) {
            roleEquipField.getField().put((short) 118, new FiedValue(118, 6000));
            desc.append("灵气:"+6000+"\n");
        }
        roleEquipField.getField().put((short) 206, new FiedValue(206, onlineMall.getType()));
        roleEquipField.getField().put((short) 40, new FiedValue(40, specialItem.getIcon()));
        roleEquipField.getField().put((short) 207, new FiedValue(207, 5));
        roleEquipField.getField().put((short) 1, new FiedValue(1, onlineMall.getName()));
        roleEquipField.getField().put((short) 306, new FiedValue(306, String.valueOf(roleEquip.getId())));
        roleEquipField.getField().put((short) 209, new FiedValue(209, specialItem.getColor()));
        roleEquipField.getField().put((short) 74, new FiedValue(74, onlineMall.getItemtype()));
        roleEquipField.getField().put((short) 84, new FiedValue(84, roleEquip.getId()));
        roleEquipField.getField().put((short) 270, new FiedValue(270, isGold ? 0 : 2));
        roleEquipField.getField().put((short) 203, new FiedValue(203, num));
        roleEquipField.getField().put((short) 282, new FiedValue(282, 0));//叠加类型
        if(onlineMall.getName().contains("金色")){
            desc = createNewGoldBook(desc,roleEquipField,isMan);
            roleEquipField.getField().put((short) 264, new FiedValue(264, desc.toString()));//叠加类型
        }
    }

    public void addGoldBook(RoleEquip roleEquip,String type,Role role){
        String qixueAdd = DataCache.REDIS_DATA.get("book_xilian_qixue");
        String wushangAdd = DataCache.REDIS_DATA.get("book_xilian_wushang");
        String fashangAdd =DataCache.REDIS_DATA.get("book_xilian_fashang");
        String suduAdd = DataCache.REDIS_DATA.get("book_xilian_sudu");
        String fangyuAdd = DataCache.REDIS_DATA.get("book_xilian_fangyu");
        int qixueM =  Integer.parseInt(DataCache.REDIS_DATA.get("book_max_qixue"));
        int wushangM = Integer.parseInt(DataCache.REDIS_DATA.get("book_max_wushang"));
        int fashangM = Integer.parseInt(DataCache.REDIS_DATA.get("book_max_fashang"));
        int suduM = Integer.parseInt(DataCache.REDIS_DATA.get("book_max_sudu"));
        int fangyuM = Integer.parseInt(DataCache.REDIS_DATA.get("book_max_fangyu"));

        int count = Integer.parseInt(DataCache.REDIS_DATA.get("book_xilian_xiaohao"));

        RoleEquipField roleEquipField = roleEquip.getFields().get((short) 1);
        FiedValue qixue = roleEquipField.getField().get((short) 7);
        int oldQixue = Integer.parseInt(qixue.getValue().toString());
        FiedValue wushang = roleEquipField.getField().get((short) 3);
        int oldWushang = Integer.parseInt(wushang.getValue().toString());
        FiedValue fashang = roleEquipField.getField().get((short) 10);
        int oldFashang = Integer.parseInt(fashang.getValue().toString());
        FiedValue fangyu = roleEquipField.getField().get((short) 8);
        int oldFangyu = Integer.parseInt(fangyu.getValue().toString());
        FiedValue sudu = roleEquipField.getField().get((short) 14);
        int oldSudu = Integer.parseInt(sudu.getValue().toString());

        int lingqi =   Integer.parseInt(roleEquipField.getField().get((short) 118).getValue().toString());
        int qixueMin = Integer.parseInt(qixueAdd.split("-")[0]);
        int qixueMax = Integer.parseInt(qixueAdd.split("-")[1]);
        int wushangMin = Integer.parseInt(wushangAdd.split("-")[0]);
        int wushangMax = Integer.parseInt(wushangAdd.split("-")[1]);
        int fashangMin = Integer.parseInt(fashangAdd.split("-")[0]);
        int fashangMax = Integer.parseInt(fashangAdd.split("-")[1]);
        int suduMin = Integer.parseInt(suduAdd.split("-")[0]);
        int suduMax = Integer.parseInt(suduAdd.split("-")[1]);
        int fangyuMin = Integer.parseInt(fangyuAdd.split("-")[0]);
        int fangyuMax = Integer.parseInt(fangyuAdd.split("-")[1]);


        int newQixue = oldQixue;
        int newWushang = oldWushang ;
        int newFashang = oldFashang;
        int newSudu = oldSudu;
        int newFangyu = oldFangyu;
        int value = 0;
        int showValue=0;
        int maxValue = 0;
        if("气血".equals(type)){
            if(oldQixue >=qixueM){
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("气血值已达上限"));
                return ;
            }
            value = qixueMin + ThreadLocalRandom.current().nextInt(qixueMax-qixueMin);
            newQixue = newQixue + value;
            if(newQixue > qixueM){
                newQixue =qixueM;
            }
            showValue = newQixue;
            maxValue = qixueM;
            roleEquipField.getField().put((short) 7, new FiedValue(7, newQixue));
        }
        if("物伤".equals(type)){
            if(oldWushang >=wushangM){
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("物伤值已达上限"));
                return ;
            }
            value =  wushangMin + ThreadLocalRandom.current().nextInt(wushangMax-wushangMin);
            newWushang = newWushang+ value;
            if(newWushang > wushangM){
                newWushang =wushangM;
            }
            showValue = newWushang;
            maxValue = wushangM;
            roleEquipField.getField().put((short) 3, new FiedValue(3, newWushang));
        }
        if("法伤".equals(type)){
            if(oldFashang >=fashangM){
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("物伤值已达上限"));
                return ;
            }
            value =  fashangMin + ThreadLocalRandom.current().nextInt(fashangMax-fashangMin);
            newFashang = newFashang + value ;
            if(newFashang > fashangM){
                newFashang =fashangM;
            }
            showValue = newFashang;
            maxValue = fashangM;
            roleEquipField.getField().put((short) 10, new FiedValue(10, newFashang));
        }
        if("速度".equals(type)){
            if(oldSudu >=suduM){
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("物伤值已达上限"));
                return ;
            }
            value =  suduMin + ThreadLocalRandom.current().nextInt(suduMax-suduMin);
             newSudu = newSudu+ value ;
            if(newSudu > suduM){
                newSudu =suduM;
            }
            showValue = newSudu;
            maxValue = suduM;
            roleEquipField.getField().put((short) 14, new FiedValue(14,newSudu));
        }
        if("防御".equals(type)){
            if(oldFangyu >=fangyuM){
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("物伤值已达上限"));
                return ;
            }
            value = fangyuMin + ThreadLocalRandom.current().nextInt(fangyuMax-fangyuMin);
             newFangyu =newFangyu +  value;
            if(newFangyu > fangyuM){
                newFangyu =fangyuM;
            }
            showValue = newFangyu;
            maxValue = newFangyu;
            roleEquipField.getField().put((short) 8, new FiedValue(8, newFangyu));
        }
        MessagePusher.pushMessage(role, new RespNotifyMiscEx("洗练成功，增加"+type+":"+value+"点，当前值："+showValue+"，最大值："+maxValue));
        SpringUtils.getBean(RoleService.class).substractChargeScore(role, count);
        StringBuffer desc = new StringBuffer();
        desc.append("灵气:"+lingqi+"\n");
        desc.append("气血:"+newQixue+"/"+qixueM+"\n");
        desc.append("物伤:"+newWushang+"/"+wushangM+"\n");
        desc.append("法伤:"+newFashang+"/"+fashangM+"\n");
        desc.append("速度:"+newSudu+"/"+suduM+"\n");
        desc.append("防御:"+newFangyu+"/"+fangyuM+"\n");
        roleEquipField.getField().put((short) 264, new FiedValue(264, desc.toString()));//叠加类型
    }
    public StringBuffer createNewGoldBook(StringBuffer desc,RoleEquipField roleEquipField,boolean isMan){
        String qixue = DataCache.REDIS_DATA.get("book_init_qixue");
        String wushang = DataCache.REDIS_DATA.get("book_init_wushang");
        String fashang = DataCache.REDIS_DATA.get("book_init_fashang");
        String sudu = DataCache.REDIS_DATA.get("book_init_sudu");
        String fangyu = DataCache.REDIS_DATA.get("book_init_fangyu");
        String qixueM = DataCache.REDIS_DATA.get("book_max_qixue");
        String wushangM = DataCache.REDIS_DATA.get("book_max_wushang");
        String fashangM = DataCache.REDIS_DATA.get("book_max_fashang");
        String suduM = DataCache.REDIS_DATA.get("book_max_sudu");
        String fangyuM = DataCache.REDIS_DATA.get("book_max_fangyu");
        int qixueMin = Integer.parseInt(qixue.split("-")[0]);
        int qixueMax = Integer.parseInt(qixue.split("-")[1]);
        int wushangMin = Integer.parseInt(wushang.split("-")[0]);
        int wushangMax = Integer.parseInt(wushang.split("-")[1]);

        int fashangMin = Integer.parseInt(fashang.split("-")[0]);
        int fashangMax = Integer.parseInt(fashang.split("-")[1]);
        int suduMin = Integer.parseInt(sudu.split("-")[0]);
        int suduMax = Integer.parseInt(sudu.split("-")[1]);
        int fangyuMin = Integer.parseInt(fangyu.split("-")[0]);
        int fangyuMax = Integer.parseInt(fangyu.split("-")[1]);
        int newQixue = 0;
        int newWushang =0;
        int newFashang = 0;
        int newSudu = 0;
        int newFangyu = 0;
        if(isMan){
            newQixue = Integer.parseInt(qixueM);
            newWushang = Integer.parseInt(wushangM);
            newFashang = Integer.parseInt(fashangM);
            newSudu = Integer.parseInt(suduM);
            newFangyu = Integer.parseInt(fangyuM);
        }else{
            newQixue = qixueMin + ThreadLocalRandom.current().nextInt(qixueMax-qixueMin);
            newWushang = wushangMin + ThreadLocalRandom.current().nextInt(wushangMax-wushangMin);
            newFashang = fashangMin + ThreadLocalRandom.current().nextInt(fashangMax-fashangMin);
            newSudu = suduMin + ThreadLocalRandom.current().nextInt(suduMax-suduMin);
            newFangyu = fangyuMin + ThreadLocalRandom.current().nextInt(fangyuMax-fangyuMin);
        }
        roleEquipField.getField().put((short) 7, new FiedValue(7, newQixue));
        roleEquipField.getField().put((short) 10, new FiedValue(10, newFashang));
        roleEquipField.getField().put((short) 14, new FiedValue(14, newSudu));
        roleEquipField.getField().put((short) 3, new FiedValue(3, newWushang));
        roleEquipField.getField().put((short) 8, new FiedValue(8, newFangyu));
        desc.append("气血:"+newQixue+"/"+qixueM+"\n");
        desc.append("物伤:"+newWushang+"/"+wushangM+"\n");
        desc.append("法伤:"+newFashang+"/"+fashangM+"\n");
        desc.append("速度:"+newSudu+"/"+suduM+"\n");
        desc.append("防御:"+newFangyu+"/"+fangyuM+"\n");
        return desc;
    }
    /**
     * 获得天书
     */

    public void buyBook(Role role, OnlineMall onlineMall, int num, boolean isGold, short pos) {
        RoleEquip roleEquip = new RoleEquip();
        roleEquip.setId(getRoleEquipId());

        roleEquip.setRoleId(role.getRoleId());
        roleEquip.setPosition(pos);
        roleEquip.setName(onlineMall.getName());
        roleEquip.setFields(new LinkedHashMap<>());
        getBook(roleEquip, onlineMall, num, isGold,false);
        roleEquip.setType("天书");
        equipService.add(role, roleEquip);
        equipService.refreshRoleEquip(role, roleEquip);
    }
    /**
     * 获得天书
     */

    public void buyManBook(Role role, OnlineMall onlineMall, int num, boolean isGold, short pos) {
        RoleEquip roleEquip = new RoleEquip();
        roleEquip.setId(getRoleEquipId());

        roleEquip.setRoleId(role.getRoleId());
        roleEquip.setPosition(pos);
        roleEquip.setName(onlineMall.getName());
        roleEquip.setFields(new LinkedHashMap<>());
        getBook(roleEquip, onlineMall, num, isGold,true);
        roleEquip.setType("天书");
        equipService.add(role, roleEquip);
        equipService.refreshRoleEquip(role, roleEquip);
    }

    private void getFashion(RoleEquip roleEquip, OnlineMall onlineMall, int num, boolean isGold) {
        SpecialItem specialItem = null;
        byte type = 2;
        if (onlineMall.getName().contains("·")) {
            String[] strings = onlineMall.getName().split("·");
            if(strings[0].contains("万道")){
                type = 3;
                specialItem = getSpecialItem(onlineMall.getName());
            }else{
                specialItem = getSpecialItem(strings[0]);
            }
        } else {
            specialItem = getSpecialItem(onlineMall.getName());
        }
        RoleEquipField roleEquipField = new RoleEquipField();
        roleEquipField.setType((short) 1);
        roleEquipField.setField(new LinkedHashMap<>());
        roleEquip.getFields().put(roleEquipField.getType(), roleEquipField);
        roleEquipField.getField().put((short) 89, new FiedValue(89, ""));
        roleEquipField.getField().put((short) 205, new FiedValue(205, 1));
        roleEquipField.getField().put((short) 261, new FiedValue(261, 0));
        roleEquipField.getField().put((short) 206, new FiedValue(206, onlineMall.getType()));
        roleEquipField.getField().put((short) 74, new FiedValue(74, onlineMall.getItemtype()));
        roleEquipField.getField().put((short) 44, new FiedValue(44, 0));
        roleEquipField.getField().put((short) 34, new FiedValue(34, 0));
        roleEquipField.getField().put((short) 202, new FiedValue(202, 16));//装备类型
        roleEquipField.getField().put((short) 40, new FiedValue(40, specialItem.getIcon()));
        roleEquipField.getField().put((short) 215, new FiedValue(215, 0));
        roleEquipField.getField().put((short) 348, new FiedValue(348, 2592000));//限制时间的秒数
        roleEquipField.getField().put((short) 207, new FiedValue(207, 500));
        roleEquipField.getField().put((short) 264, new FiedValue(264, ""));
        roleEquipField.getField().put((short) 1, new FiedValue(1, onlineMall.getName().contains("天")?onlineMall.getName().split("·")[0]:onlineMall.getName()));
        roleEquipField.getField().put((short) 306, new FiedValue(306, String.valueOf(roleEquip.getId())));
        roleEquipField.getField().put((short) 38, new FiedValue(38, 0));
        roleEquipField.getField().put((short) 84, new FiedValue(84, roleEquip.getId()));
        roleEquipField.getField().put((short) 209, new FiedValue(209, specialItem.getColor()));
        roleEquipField.getField().put((short) 349, new FiedValue(349,type));//时装类型
        roleEquipField.getField().put((short) 270, new FiedValue(270, isGold ? 0 : 2));
        roleEquipField.getField().put((short) 203, new FiedValue(203, num));
        roleEquipField.getField().put((short) 41, new FiedValue(7, 8));
        roleEquipField.getField().put((short) 3001, new FiedValue(3001, onlineMall.getName()));
        roleEquipField.getField().put((short) 887, new FiedValue(887,0));
        roleEquipField.getField().put((short) 29, new FiedValue(29, specialItem.getSex()));
        roleEquipField.getField().put((short) 282, new FiedValue(282, Const.ITEM_COMBINED_EX));//叠加类型
    }

    /**
     * 购买时装
     */
    public void buyFashion(Role role, OnlineMall onlineMall, int num, boolean isGold, short pos) {
        RoleEquip roleEquip = new RoleEquip();
        roleEquip.setId(getRoleEquipId());

        roleEquip.setRoleId(role.getRoleId());
        roleEquip.setPosition(pos);
        roleEquip.setName(onlineMall.getName());
        roleEquip.setFields(new LinkedHashMap<>());
        getFashion(roleEquip, onlineMall, num, isGold);
        roleEquip.setType("时装");

        equipService.add(role, roleEquip);
        equipService.refreshRoleEquip(role, roleEquip);
    }

    /*飞行器*/
    public void buyFeixingqi(Role role, OnlineMall onlineMall, int num, boolean isGold, short pos) {

        RoleEquip roleEquip = new RoleEquip();
        roleEquip.setId(getRoleEquipId());

        roleEquip.setRoleId(role.getRoleId());
        roleEquip.setPosition(pos);
        roleEquip.setName(onlineMall.getName());
        roleEquip.setFields(new LinkedHashMap<>());
        getFeixingqi(roleEquip, onlineMall, num, isGold);
        roleEquip.setType("飞行器");

        equipService.add(role, roleEquip);
        equipService.refreshRoleEquip(role, roleEquip);
    }

    private void getFeixingqi(RoleEquip roleEquip, OnlineMall onlineMall, int num, boolean isGold) {

        SpecialItem specialItem = getSpecialItem(onlineMall.getName());
        log.info("specialItem={}", JSONObject.toJSONString(specialItem));
        RoleEquipField roleEquipField = new RoleEquipField();
        roleEquipField.setType((short) 1);
        roleEquipField.setField(new LinkedHashMap<>());
        roleEquip.getFields().put(roleEquipField.getType(), roleEquipField);

        roleEquipField.getField().put((short) 74, new FiedValue(74, onlineMall.getItemtype()));
        roleEquipField.getField().put((short) 284, new FiedValue(284, 1));
        roleEquipField.getField().put((short) 202, new FiedValue(202, 21)); // 装备位置
        roleEquipField.getField().put((short) 40, new FiedValue(40, specialItem.getIcon()));
        roleEquipField.getField().put((short) 348, new FiedValue(348, 2592000));
        roleEquipField.getField().put((short) 207, new FiedValue(207, 500));
        roleEquipField.getField().put((short) 1, new FiedValue(1, roleEquip.getName()));
        roleEquipField.getField().put((short) 306, new FiedValue(306, String.valueOf(roleEquip.getId())));
        roleEquipField.getField().put((short) 209, new FiedValue(209, specialItem.getColor()));
        roleEquipField.getField().put((short) 84, new FiedValue(84, roleEquip.getId()));
        roleEquipField.getField().put((short) 270, new FiedValue(270, 0));
        roleEquipField.getField().put((short) 283, new FiedValue(283, roleEquip.getQilingstate())); //  启灵状态 00=未启灵 01=启灵

        roleEquipField.getField().put((short) 3001, new FiedValue(3001, roleEquip.getName()));

    }


    public void qiling(Role role, OnlineMall onlineMall, int num, boolean isGold, short pos) {

        RoleEquip roleEquip = new RoleEquip();
        roleEquip.setId(getRoleEquipId());

        roleEquip.setRoleId(role.getRoleId());
        roleEquip.setPosition(pos);
        roleEquip.setName(onlineMall.getName());
        // 启灵状态
        roleEquip.setQilingstate((byte) 1);
        roleEquip.setFields(new LinkedHashMap<>());
        qiling(roleEquip, onlineMall, num, isGold);
        roleEquip.setType("飞行器");


        equipService.add(role, roleEquip);
        equipService.refreshRoleEquip(role, roleEquip);
    }

    private void qiling(RoleEquip roleEquip, OnlineMall onlineMall, int num, boolean isGold) {

        SpecialItem specialItem = getSpecialItem(onlineMall.getName());
        RoleEquipField roleEquipField = new RoleEquipField();
        roleEquipField.setType((short) 1);
        roleEquipField.setField(new LinkedHashMap<>());
        roleEquip.getFields().put(roleEquipField.getType(), roleEquipField);

        roleEquipField.getField().put((short) 74, new FiedValue(74, onlineMall.getItemtype()));
        roleEquipField.getField().put((short) 284, new FiedValue(284, 1));
        roleEquipField.getField().put((short) 202, new FiedValue(202, 21)); // 装备位置
        roleEquipField.getField().put((short) 40, new FiedValue(40, specialItem.getIcon()));
        roleEquipField.getField().put((short) 348, new FiedValue(348, 2592000));
        roleEquipField.getField().put((short) 207, new FiedValue(207, 500));
        roleEquipField.getField().put((short) 1, new FiedValue(1, roleEquip.getName()));
        roleEquipField.getField().put((short) 306, new FiedValue(306, String.valueOf(roleEquip.getId())));
        roleEquipField.getField().put((short) 209, new FiedValue(209, specialItem.getColor()));
        roleEquipField.getField().put((short) 84, new FiedValue(84, roleEquip.getId()));
        roleEquipField.getField().put((short) 270, new FiedValue(270,0));
        roleEquipField.getField().put((short) 283, new FiedValue(283, roleEquip.getQilingstate())); //  启灵状态 00=未启灵 01=启灵
//        int time = new Long((int)(System.currentTimeMillis()/1000) + 30 * 86400).intValue();
        roleEquipField.getField().put((short) 887, new FiedValue(887,0));

        roleEquipField.getField().put((short) 3001, new FiedValue(3001, roleEquip.getName()+"·30天"));

    }

}
